package com.asp.alumnisystem.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.asp.alumnisystem.R;
import com.asp.alumnisystem.app.App;
import com.asp.alumnisystem.base.BaseFragment;
import com.asp.alumnisystem.model.UserModel;
import com.asp.alumnisystem.ui.LoginActivity;
import com.asp.alumnisystem.ui.PersonalActivity;
import com.asp.alumnisystem.utils.DialogUtils;
import com.asp.alumnisystem.utils.UiUtils;
import com.asp.alumnisystem.view.ShapeImageView;
import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 首页我的页面
 */
public class MyFragment extends BaseFragment {

    @BindView(R.id.image_view)
    ShapeImageView imageView;

    @BindView(R.id.name_view)
    TextView nameView;

    @BindView(R.id.unit_view)
    TextView unitView;

    @BindView(R.id.login_out_btn)
    Button loginOptBtn;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.my_fragment_layout, container, false);
    }

    @Override
    protected void initView() {
        UserModel userModel = App.getApp().getUserModel();
        Glide.with(this).load(userModel.getIconPath()).into(imageView);
        nameView.setText(userModel.getNickName());
        unitView.setText(userModel.getCompany());
        loginOptBtn.setText(userModel.isVisitor() ? "登录" : "注销");
    }

    @OnClick(R.id.login_out_btn)
    public void loginOptBtnClick(){
        if (App.getApp().isVisitor()){
            UiUtils.jumpToPageAndFinishSelf(getActivity(), LoginActivity.class);
        }else{
            DialogUtils.showSelectDialog(getContext(), "确定要注销吗？",
                    "确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            UiUtils.jumpToPageAndFinishSelf(getActivity(), LoginActivity.class);
                        }
                    },
                    "取消", null);
        }
    }

    @OnClick(R.id.image_view)
    public void toPersonalActivity(){
        //点击头像今进入个人中心
        Bundle bundle = new Bundle();
        bundle.putSerializable("model", App.getApp().getUserModel());
        UiUtils.jumpToPage(getContext(), PersonalActivity.class, bundle);
    }

    @Override
    protected void destroy() {

    }
}
